package com.zcy.auth.controller;

import com.zcy.auth.dto.LoginDTO;
import com.zcy.auth.service.ILoginService;
import com.zcy.auth.vo.LoginVO;
import com.zcy.basic.data.ContextData;
import com.zcy.basic.util.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *
 * </p>
 *
 * @author lucker
 * @date 2023/12/5 9:55
 */
@RestController
@RequestMapping("/login")
@Api(value = "登录相关API", description = "登录相关API")
public class LoginController {

    @Autowired
    private ILoginService loginService;

    @PostMapping
    @ApiOperation(value = "登录", notes = "登录")
    public AjaxResult login(@RequestBody LoginDTO loginDTO) {
        try {
            LoginVO loginVO = loginService.login(loginDTO);
            return AjaxResult.success(loginVO);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    @GetMapping("/out")
    @ApiOperation(value = "退出登录", notes = "退出登录")
    public AjaxResult logOut(@RequestHeader("token") String token) {
        if (token != null && !token.isEmpty()) {
            ContextData.LOGIN_MAP.remove(token);
        }
        return AjaxResult.success("退出成功");
    }
}
